package com.oracle.application.controller;
import com.oracle.application.controller.vo.R;
import com.oracle.application.entity.Book;
import com.oracle.application.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wushaoxiong
 * @since 2021-07-06
 */
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private IBookService iBookService;

    @GetMapping("/getBookName")
    public R getBooksByName(@RequestParam("bookName")String bookName){
        Book book=this.iBookService.selectBookByName(bookName);
        return new R<>(200,"success",book);
    }

    @GetMapping("/list")
    public R list(){
        return new R(200,"success",this.iBookService.list());
    }


    @PostMapping("/addBook")
    public R add(@RequestBody Book book){
        this.iBookService.save(book);
        return new R(200,"success",book);
    }

    @PutMapping("/update")
    public R update(@RequestBody Book book){
        this.iBookService.saveOrUpdate(book);
        return new R(200,"success",book);
    }

    @DeleteMapping("/deleteById")
    public R delete(@RequestParam("id") Integer id){
        this.iBookService.removeById(id);
        return new R(200,"success",null);

    }


}
